package com.fog.judge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fog.judge.entity.Course;
import com.fog.judge.entity.User;
import com.fog.judge.mapper.CourseMapper;
import com.fog.judge.mapper.UserMapper;
import com.fog.judge.service.CourseService;
import com.fog.judge.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    public boolean existsByUsername(String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        return this.baseMapper.selectCount(queryWrapper) > 0;
    }

    @Override
    public boolean deleteUserByUsername(String username) {
        if(!existsByUsername(username)){
            return false;
        }
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        boolean result = this.remove(queryWrapper);
        return result;
    }

    @Override
    public User findUserById(int id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public void deleteUser(String ids) {
        String[] list = ids.split(",");
        for (String id : list) {
            removeById(Integer.parseInt(id));
        }
    }
}
